package myzzyl.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import myzzyl.constants.ErrorConstants;
import myzzyl.domain.dto.FloorDto;
import myzzyl.domain.pojo.Floor;
import myzzyl.domain.vo.FloorVo;
import myzzyl.domain.vo.RoomVo;
import myzzyl.exception.BusinessException;
import myzzyl.mapper.FloorMapper;
import myzzyl.service.FloorService;
import myzzyl.service.RoomService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author 33249
* @description 针对表【floor】的数据库操作Service实现
* @createDate 2025-08-07 20:07:25
*/
@Service
public class FloorServiceImpl extends ServiceImpl<FloorMapper, Floor>
    implements FloorService {

    /**
     * 获取所有楼层
     * @return 返回值
     */
    @Override
    public List<Floor> getAll() {
        return list();
    }

    /**
     * 获取楼层详情
     * @param id 楼层id
     * @return 返回值
     */
    @Override
    public Floor getInfo(Long id) {
        // 根据id获取对象
        Floor floor = getById(id);
        if(floor == null) {
            throw new BusinessException(ErrorConstants.FLOOR_IS_NOT_EXIST);
        }
        return floor;
    }


    /**
     * 新增楼层
     * @param floorDto 楼层表单封装类
     * @return 返回值
     */
    @Override
    public Boolean add(FloorDto floorDto) {
        // 查询楼层是否已经存在
        if(lambdaQuery().eq(StrUtil.isNotBlank(floorDto.getName()), Floor::getName, floorDto.getName()).exists()) {
            throw new BusinessException(ErrorConstants.FLOOR_IS_ALREADY_EXIST);
        }
        // 添加楼层
        Floor floor = new Floor();
        BeanUtils.copyProperties(floorDto, floor);
        floor.setCreateTime(new Date());
        floor.setUpdateTime(new Date());
        floor.setCreateBy(1L);
        floor.setUpdateBy(1L);

        return save(floor);
    }

    /**
     * 编辑楼层
     * @param floorDto 楼层表单封装类
     * @return 返回值
     */
    @Override
    public Boolean edit(FloorDto floorDto) {
        // 查询楼层是否已经存在（除去自己）
        if(lambdaQuery()
                .eq(StrUtil.isNotBlank(floorDto.getName()), Floor::getName, floorDto.getName())
                .ne(floorDto.getId() != null, Floor::getId, floorDto.getId())
                .exists()) {
            throw new BusinessException(ErrorConstants.FLOOR_IS_ALREADY_EXIST);
        }

        // 修改楼层
        return lambdaUpdate()
                .set(StrUtil.isNotEmpty(floorDto.getName()), Floor::getName, floorDto.getName())
                .set(floorDto.getCode() != null, Floor::getCode, floorDto.getCode())
                .eq(Floor::getId, floorDto.getId())
                .update();
    }


    /**
     * 删除楼层
     * @param id 楼层id
     * @return 返回值
     */
    @Override
    public Boolean deleteFloorById(Long id) {
        // 判断要删除的楼层是否不存在
        if(!lambdaQuery().eq(id != null, Floor::getId, id).exists()) {
            throw new BusinessException(ErrorConstants.FLOOR_IS_NOT_EXIST);
        }
        //todo 判断要删除的楼层下是否有房间

        // 删除楼层
        return baseMapper.deleteById(id) > 0;
    }

    /**
     * 获取所有楼层下的房间和床位
     * @return 返回值
     */
    @Override
    public List<FloorVo> getAllWithRoomAndBed() {
        return baseMapper.getAllWithRoomAndBed();
    }
}




